﻿using Dapper;
using Domain.Entity.Basic_information;
using Domain.Entity.System_operation;
using Domain.IRepository.Vehicle_tracks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.Vehicle_tracks
{
    [Dependency(ServiceLifetime.Scoped)]
    public class TracksRepository : ITrackRepository
    {

        private MyDbContext myDbContext { get; set; }

        public TracksRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public List<Wb_meter> GetMeters(string name)
        {
            string sql = "SELECT * FROM dbo.Wb_meter W, dbo.Drivers_ment D WHERE D.Dv_id=W.Dv_id";
            if(name != null && name != "")
            {
                sql += $" AND Dv_name LIKE '%{name}%'";
            }
            return myDbContext.Database.GetDbConnection().Query<Wb_meter>(sql).ToList();
        }

        public int GetCount(string name)
        {
            string sql = "SELECT Wb_number FROM dbo.Wb_meter W, dbo.Drivers_ment D WHERE D.Dv_id=W.Dv_id";
            if (name != null && name != "")
            {
                sql += $" AND Dv_name LIKE '%{name}%'";
            }
            return myDbContext.Database.GetDbConnection().Query<Wb_meter>(sql).Count();
        }

        public Drivers_ment GetDrivers(int id)
        {
            string sql = $"SELECT Dv_name FROM dbo.Drivers_ment WHERE Dv_id={id}";
            return myDbContext.Database.GetDbConnection().Query<Drivers_ment>(sql).FirstOrDefault();
        }

        public Vehicles_ment GetVehicles(int id)
        {
            string sql = $"SELECT Vc_phone FROM dbo.Vehicles_ment WHERE Vc_id={id}";
            return myDbContext.Database.GetDbConnection().Query<Vehicles_ment>(sql).FirstOrDefault();
        }

        public Wb_meter GetMeter(int id)
        {
            string sql = $"SELECT * FROM dbo.Wb_meter WHERE Wb_id={id}";
            return myDbContext.Database.GetDbConnection().Query<Wb_meter>(sql).FirstOrDefault();
        }
    }
}
